<template>
  <div class="app-container">
    <el-row>
      <el-button type="primary" @click="addBankAddress" plain
        icon="el-icon-plus" style="margin-bottom: 20px">添加银行地址</el-button>
    </el-row>
    <el-table ref="table" :data="tableData">
      <el-table-column prop="areaName" align="center" label="银行地址"> </el-table-column>
      <el-table-column prop="createTime" align="center" label="创建时间"> </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="editBankAddress(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete"
            @click="delBankAddress(scope.row.areaId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="margin-top: 20px" background @size-change="handleSizeChange"
      @current-change="handleCurrentChange" :current-page="page.pageNum" :page-sizes="[10, 20, 50]"
      :page-size="page.pageSize" layout="->, total, sizes, prev, pager, next, jumper" :total="page.total">
    </el-pagination>
    <el-dialog :title="vipTitle" :visible.sync="dialogVisible" width="40%">
      <el-form :model="addBankAddressForm" ref="addBankAddressForm" :rules="rules" label-width="140px">
        <el-form-item label="银行地址" style="width: 100%" prop="areaName">
          <el-input v-model="addBankAddressForm.areaName" placeholder="银行地址"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cance">取 消</el-button>
        <el-button type="primary" @click="determine">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      vipTitle: "添加银行地址",
      editId: "",
      addBankAddressForm: {
        areaName: "",
      },
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      dialogVisible: false,
      // form: {
      //     user: '',
      //     region: ''
      // },
      tableData: [],
      tableHeight: 0,
      rules: {
        areaName: [
          { required: true, message: "请输入银行地址", trigger: "blur" },
        ]
      },
    };
  },
  methods: {
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getTableData();
    },
    setTableHeight() {
      this.$nextTick(() => {
        this.tableHeight =
          window.innerHeight - 240 + "px";
      });
    },
    determine() {
      this.$refs.addBankAddressForm.validate((valid) => {
        if (valid) {
          const url = this.editId ? "/config/area" : "/config/area";
          const func = this.editId ? "$put" : "$post";
          const params = this.editId
            ? { ...this.addBankAddressForm, areaId: this.editId }
            : this.addBankAddressForm;
          this[func](url, params).then((res) => {
            if (res && res.code * 1 === 200) {
              this.$message({
                type: "success",
                message: this.editId ? "修改成功" : "新增成功",
              });
              this.$refs.addBankAddressForm.resetFields();
              this.dialogVisible = false;
              this.getTableData();
            }
          });
        }
      });
    },
    delBankAddress(areaId) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$delete(`/config/area/${areaId}`).then((res) => {
            if (res && res.code * 1 === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getTableData();
            }
          });
        })
        .catch(() => {
          //
        });
    },
    cance() {
      this.$refs.addBankAddressForm.resetFields();
      this.dialogVisible = false;
    },
    addBankAddress() {
      this.$nextTick(() => {
        this.vipTitle = "添加银行地址";
        this.dialogVisible = true;
        this.addBankAddressForm.areaName = "";
        this.editId = "";
      });
    },
    editBankAddress(row) {
      this.$nextTick(() => {
        this.vipTitle = "修改银行地址";
        this.dialogVisible = true;
        this.addBankAddressForm.areaName = row.areaName;
        this.editId = row.areaId;
      });
    },
    getTableData() {
      this.$get("/config/area/list", {
        pageNum: this.page.pageNum,
        pageSize: this.page.pageSize,
      }).then((res) => {
        this.tableData = res.rows;
        this.page.total = res.total;
      });
    },
  },
  mounted() {
    window.onresize = () => {
      this.setTableHeight();
    };
    this.setTableHeight();
    this.getTableData();
  },
};
</script>
<style scoped lang="scss">
.form-inline ::v-deep {
  width: 100%;

  .el-form-item__label {
    // padding: 0;
  }
}
</style>
  